<?php
require_once("lib.php");

// Basic set has occured.
// Use $_SESSION["authed"] to test for auth
// Use $db to access mysqli API to database

if (!$_SESSION["authed"]) {
    header("index.php");
}
$tables = array();
$req = $db->query("SHOW TABLES");
while ($row = $req->fetch()) {
    $table = $row["Tables_in_tracks"];
    if (preg_match('/^track_[a-f0-9]{32}$/', $table)) {
        $tables[] = $table;
    }
}

$tableNames = array();
$req = $db->query("SELECT tableName FROM track_tables_info");
while ($row = $req->fetch()) {
    $tableNames[] = $row["tableName"];
}

//A list of tables which exist, but are not in track_tables_info
$tablesExist = "";
foreach (array_diff($tables, $tableNames) as $item) {
    $tablesExist .= "<li>$item</li>";
}


//A list of tables references which exist, but no tables are found
$tablesRefsExist = "";
foreach (array_diff($tableNames, $tables) as $item) {
    $tablesRefsExist .= "<li>$item</li>";
}


$htmlTitle = "Hikes and Things! - Table Manager";
echo <<<CONTENT
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>$htmlTitle</title>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="style.css" rel="stylesheet">
        $analytics
    </head>
    <body>
        <div class="navbar navbar-static">
            <div class="navbar-inner">
                <div class="container" style="width: auto;">
                    <a class="brand" href="#">Table Manager</a>
                    <ul class="nav" role="navigation">
                        <li class="divider-vertical"></li>
                        <li><a href="index.php">Tracks</a></li>
                    </ul>
                    <ul class="nav pull-right">
                        <li class="divider-vertical"></li>
                        <li>$logInOut</li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="span5">
            <div class="well">
                <p>A list of tables which exist, but are not in track_tables_info</p>
                <ul>
                    $tablesExist
                </ul>
            </div>
        </div>
        <div class="span5">
            <div class="well">
                <p>A list of tables references which exist, but no tables are found</p>
                <ul>
                    $tablesRefsExist
                </ul>
            </div>
        </div>
    </body>
</html>
CONTENT;
?>
